//
//  HNHandleCourseTeacherCell.m
//  优学荟管理系统
//
//  Created by edz on 2018/1/6.
//  Copyright © 2018年 星道三好. All rights  reserved    .
//

#import "HNHandleCourseTeacherCell.h"

@implementation HNHandleCourseTeacherCell {
    
    
    __weak IBOutlet UIButton *_btn;
    __weak IBOutlet UIButton *_delete;
    UILongPressGestureRecognizer *_LongPress;
}

- (void)setModel:(TeacherCourseSelectModel *)model {
    
    _model = model;
    
    _btn.selected = model.isLastObject ? NO:YES;
    [_btn setTitle:model.isLastObject ? @"":model.petName forState:(UIControlStateSelected)];
    _btn.titleLabel.font = [UIFont systemFontOfSize:12.5f];
    _delete.hidden = model.isHideDeleteImg;
    // 设置长按手势
    if (model.isLastObject == NO) {
        [self LongPress];
    } else {
        [self removeGestureRecognizer:_LongPress];
    }
    

}

/** 长按手势的触发 */
- (void)CellLongPressGestureRecognizer:(UILongPressGestureRecognizer *)longPress {
    
    if (longPress.state == UIGestureRecognizerStateBegan) {
        
        // 这个实现是显示全部数据的删除按钮出来
        self.LongPressAction ? self.LongPressAction(self.model.isHideDeleteImg) : nil;
    }
}

/** 删除 */
- (IBAction)deleteTeacher:(UIButton *)sender {
    
    self.deleteTeacherAction ? self.deleteTeacherAction (self.model) : nil;
}


- (UILongPressGestureRecognizer *)LongPress {
    
    if (!_LongPress) {
        
        _LongPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(CellLongPressGestureRecognizer:)];
        _LongPress.minimumPressDuration = 0.8f;
        _LongPress.numberOfTouchesRequired = 1;
        [self addGestureRecognizer:_LongPress];
    }
    return _LongPress;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    // 展示抖动效果
    if (self.model.isHideDeleteImg == NO) {
        
        [self shakeAnimationForView:_delete];
    }
}


/**
 *  抖动效果
 *
 *  @param view 要抖动的view
 */
- (void)shakeAnimationForView:(UIView *) view {
    CALayer *viewLayer = view.layer;
    CGPoint position = viewLayer.position;
    CGPoint x = CGPointMake(position.x + 1, position.y);
    CGPoint y = CGPointMake(position.x - 1, position.y);
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    [animation setFromValue:[NSValue valueWithCGPoint:x]];
    [animation setToValue:[NSValue valueWithCGPoint:y]];
    [animation setAutoreverses:YES];
    [animation setDuration:.06];
    [animation setRepeatCount:3];
    [viewLayer addAnimation:animation forKey:nil];
}
@end
